package com.apache.flow.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.apache.common.FlowBaseAction;
import com.apache.core.orm.Page;
import com.apache.core.utils.web.struts2.Struts2Utils;
import com.apache.flow.core.entity.FlowAutomaticProcessing;
import com.apache.flow.core.manager.FlowAutomaticProcessingManager;
import com.apache.tools.NumberUtils;
import com.apache.tools.StrUtil;

@Namespace("/flow/core")
@Results({})
public class FlowAutomaticAction extends FlowBaseAction {

	private static final long serialVersionUID = 1L;
	private Logger logger = LoggerFactory.getLogger(FlowAutomaticAction.class);
	@Autowired
	private FlowAutomaticProcessingManager automaticProcessingManager;

	private String rows;
	private String page;
	private String taskId;
	private String handleStauts;
	private String processInsId;

	private Page<FlowAutomaticProcessing> pageFlowAuto = new Page<FlowAutomaticProcessing>();

	public String data() {
		int pageNo = NumberUtils.getInt(page, 1);
		int pageSize = NumberUtils.getInt(rows, 15);
		pageFlowAuto.setPageNo(pageNo);
		pageFlowAuto.setPageSize(pageSize);
		Map<String, String> map = new HashMap<String, String>();
		if (StrUtil.isNotNull(taskId))
			map.put("taskId", taskId);
		if (StrUtil.isNotNull(handleStauts))
			map.put("handleStauts", handleStauts);
		if (StrUtil.isNotNull(processInsId))
			map.put("processInsId", processInsId);
		pageFlowAuto = this.automaticProcessingManager.getFlowAutomaticProcessingPage(pageFlowAuto, map);
		JSONObject json = new JSONObject();
		json.put("total", pageFlowAuto.getTotalCount());
		json.put("rows", JSONArray.fromObject(pageFlowAuto.getResult()));
		try {
			Struts2Utils.getResponse().setContentType("text/html;charset=UTF-8");
			Struts2Utils.getResponse().getWriter().write(json.toString());
		} catch (IOException e) {
			logger.debug("得到自处理任务数据错误" + e);
			e.printStackTrace();
		}
		return null;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public Page<FlowAutomaticProcessing> getPageFlowAuto() {
		return pageFlowAuto;
	}

	public void setPageFlowAuto(Page<FlowAutomaticProcessing> pageFlowAuto) {
		this.pageFlowAuto = pageFlowAuto;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getHandleStauts() {
		return handleStauts;
	}

	public void setHandleStauts(String handleStauts) {
		this.handleStauts = handleStauts;
	}

	public String getProcessInsId() {
		return processInsId;
	}

	public void setProcessInsId(String processInsId) {
		this.processInsId = processInsId;
	}
}
